package com.ruoyi.car.business.web.controller;

import com.ruoyi.car.business.domain.BusBpmnInfo;
import com.ruoyi.car.business.service.IBusBpmnInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 流程定义明细Controller
 * 
 * @author ruoyi
 * @date 2022-02-03
 */
@Controller
@RequestMapping("business/bpmnInfo")
public class BusBpmnInfoController extends BaseController
{
    private String prefix = "business/bpmnInfo/";

    @Autowired
    private IBusBpmnInfoService busBpmnInfoService;

    @RequiresPermissions("business:bpmnInfo:view")
    @GetMapping("/listPage")
    public String info()
    {
        return prefix + "list";
    }

    /**
     * 查询流程定义明细列表
     */
    @RequiresPermissions("business:bpmnInfo:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusBpmnInfo busBpmnInfo)
    {
        startPage();
        List<BusBpmnInfo> list = busBpmnInfoService.selectBusBpmnInfoList(busBpmnInfo);
        return getDataTable(list);
    }

    /**
     * 删除流程定义明细
     */
    @RequiresPermissions("business:bpmnInfo:remove")
    @Log(title = "流程定义明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(busBpmnInfoService.deleteBusBpmnInfoByIds(ids));
    }

    /**
     * 删除流程定义文件
     */
    @RequiresPermissions("business:bpmnInfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        busBpmnInfoService.deleteBusBpmnInfoById(id);
        return AjaxResult.success();
    }

    /**
     * 流程文件部署
     * @return
     */
    @RequiresPermissions("business:bpmnInfo:deployPage")
    @RequestMapping("/deployPage")
    public String deployPage(){
        return prefix + "deploy";
    }

    /**
     * 流程文件部署
     */
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(String path,String bpmnType,String info) throws Exception {
        busBpmnInfoService.deploy(path,bpmnType,info);
        return AjaxResult.success();
    }

    /**
     * 部署文件上传
     */
    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws IOException {
        String filePath = busBpmnInfoService.upload(file);
        //System.out.println(filePath);
        return AjaxResult.success("文件上传成功",filePath);
    }

    /**
     * 查看资源
     */
    @RequestMapping( "/readResource")
    @ResponseBody
    public void readResource(HttpServletResponse response, String deploymentId, String type) throws IOException {
        InputStream inputStream  =  busBpmnInfoService.getFileInputStream(deploymentId, type);
        IOUtils.copy(inputStream,response.getOutputStream());
    }
}
